﻿using Power.Models;
using Power.View.Report;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.ViewModel.Report
{
    /// <summary>
    /// 缺陷管段表
    /// </summary>
    public class DefectSectionTableViewModel : ReportTableBase
    {
        public List<DefectPipnetSegTableData> Data { get; set; }
        private DefectSectionTable model;
        public DefectSectionTableViewModel(DefectSectionTable model)
        {
            this.model = model;
            Data = CreateEntity(2, 5);

            Init();
        }

        /// <summary>
        /// 动态创建测试数据
        /// </summary>
        /// <param name="subCount"></param>
        /// <returns></returns>
        public List<DefectPipnetSegTableData> CreateEntity(int count,int subCount)
        {

            var list = new List<DefectPipnetSegTableData>();
            for (int i = 0; i < count; i++)
            {
                var obj = Activator.CreateInstance(typeof(DefectPipnetSegTableData));

                var type = obj.GetType();
                var props = type.GetProperties();
                foreach (var item in props)
                {
                    if (item.PropertyType == typeof(int))
                    {
                        item.SetValue(obj, 1);
                    }
                    else if (item.PropertyType == typeof(string))
                    {
                        item.SetValue(obj, "2");
                    }
                }
                var ret = obj as DefectPipnetSegTableData;
                ret.BaseValues = new List<DefectPipnetSegTableBaseValue>();
                for (int j = 0; j < subCount; j++)
                {
                    ret.BaseValues.Add(CreateSubEntity());
                }
                list.Add(ret);
            }
            

            
            return list;
        }
        /// <summary>
        /// 动态创建子项测试数据
        /// </summary>
        /// <returns></returns>
        public DefectPipnetSegTableBaseValue CreateSubEntity()
        {
            var obj = Activator.CreateInstance(typeof(DefectPipnetSegTableBaseValue));

            var type = obj.GetType();
            var props = type.GetProperties();
            foreach (var item in props)
            {
                if (item.PropertyType == typeof(int))
                {
                    item.SetValue(obj, 1);
                }
                else if (item.PropertyType == typeof(string))
                {
                    item.SetValue(obj, "2");
                }
            }
            var ret = obj as DefectPipnetSegTableBaseValue;
            ret.LevelValues = new List<DefectPipnetSegTableLevelValue>()
            {
                CreateSubEntity2(),
                 CreateSubEntity2(),
            };
            return ret;
        }
        public DefectPipnetSegTableLevelValue CreateSubEntity2()
        {
            var obj = Activator.CreateInstance(typeof(DefectPipnetSegTableLevelValue));

            var type = obj.GetType();
            var props = type.GetProperties();
            foreach (var item in props)
            {
                if (item.PropertyType == typeof(int))
                {
                    item.SetValue(obj, 1);
                }
                else if (item.PropertyType == typeof(string))
                {
                    item.SetValue(obj, "2");
                }
            }
            var ret = obj as DefectPipnetSegTableLevelValue;
            return ret;
        }
        public void Init()
        {
            Row =  Data.SelectMany(p=>p.BaseValues).SelectMany(p=>p.LevelValues).Count() + 3;

            Column = 5;
            var table = this.model.table;

            for (int i = 0; i < Row; i++)
            {
                this.model.table.RowDefinitions.Add(new RowDefinition());

            }
            for (int i = 0; i < Column; i++)
            {
                if (i == 3)
                {
                    table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(500) });

                }
                else
                    this.model.table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(150) });
            }

            //画标题
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < Column; j++)
                {
                    DrawColumnSpanCell(table, i, j, "缺陷管段表", Column, 0, 0);

                    DrawRowSpanCell(table, i, j, "缺陷类型", 2, 1, 0);
                    DrawRowSpanCell(table, i, j, "缺陷名称", 2, 1, 1);
                    DrawRowSpanCell(table, i, j, "缺陷等级", 2, 1, 2);
                    DrawRowSpanCell(table, i, j, "缺陷管段", 2, 1, 3);


                    DrawCell(table, i, j, "管段数量", 1, 4);
                    DrawCell(table, i, j, "段", 2, 4);

                }
            }
            
            var firstRow = 3;
            var secondRow = 3;
            var currentRow = 3;
            for (int i = 0; i < Data.Count; i++)
            {
                var item = Data[i];
                for (int j = 0; j < item.BaseValues.Count; j++)
                {
                    var subItem = item.BaseValues[j];
                    for (int k = 0; k < subItem.LevelValues.Count; k++)
                    {
                        var row = subItem.LevelValues[k];

                        var columnIndex = 0;
                        DrawRowSpanCell(this.model.table, currentRow, columnIndex, item.DefectType, item.BaseValues.SelectMany(p=>p.LevelValues).Count(), firstRow, columnIndex++);
                        DrawRowSpanCell(this.model.table, currentRow, columnIndex, subItem.DefectName, subItem.LevelValues.Count(), secondRow, columnIndex++);

                        DrawCell(this.model.table, currentRow, columnIndex, row.DefectLev, currentRow, columnIndex++);
                        DrawCell(this.model.table, currentRow, columnIndex, row.PipeSections.ToString(), currentRow, columnIndex++);
                        DrawCell(this.model.table, currentRow, columnIndex, row.PipeNum.ToString(), currentRow, columnIndex++);
                        currentRow++;
                    }
                    secondRow = currentRow;
                    
                }

                firstRow = currentRow;
            }


            //补边框
            DrawBorder(table);
        }
    }
}
